package com.example.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.example.broadcast.receiver.StandardReceiver;

import java.util.logging.Filter;

public class StandardBroadCastActivity extends AppCompatActivity implements View.OnClickListener {

    private StandardReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard_broadcast);
        findViewById(R.id.btn_broadcast).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(StandardReceiver.STANDARD_ACTION);
        //发送广播
        sendBroadcast(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        receiver = new StandardReceiver();
        // 创建一个意图过滤器，只处理STANDARD_ACTION的广播
        IntentFilter filter = new IntentFilter(StandardReceiver.STANDARD_ACTION);
        registerReceiver(receiver,filter);
        Log.d("tang", "onStart: 注册成功");
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(receiver);
    }
}